package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IWxCustomerProductService;
import com.mdd.admin.validate.WxCustomerCreateValidate;
import com.mdd.admin.validate.WxCustomerSearchValidate;
import com.mdd.admin.validate.WxCustomerUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.WxCustomerVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.WxCustomer;
import com.mdd.common.mapper.ShopMapper;
import com.mdd.common.mapper.WxCustomerMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
@Service
public class WxCustomerProductServiceImpl implements IWxCustomerProductService {
    @Resource
    WxCustomerMapper    wxCustomerMapper;
    @Resource
    ShopMapper  shopMapper;

    /**
     * 客服查询
     * @param pageValidate
     * @param searchValidate
     * @return
     */
    @Override
    public PageResult<WxCustomerVo> list(PageValidate pageValidate, WxCustomerSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();
        Integer shopId = LikeAdminThreadLocal.getShopId();
        QueryWrapper<WxCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        if(null != shopId && shopId > 1) {
            queryWrapper.eq("shop_id", shopId);
        }

        if (StringUtils.isNotNull(searchValidate.getWxId())) {
            queryWrapper.like("wx_id", searchValidate.getWxId());
        }
        if (StringUtils.isNotNull(searchValidate.getWxName())) {
            queryWrapper.like("wx_name", searchValidate.getWxName());
        }
        wxCustomerMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "like:wxId@wx_id:str",
                "like:wxName@wx_name:str"
        });
        Page<WxCustomer> iPage = wxCustomerMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<WxCustomerVo> list = new LinkedList<>();
        for(WxCustomer item : iPage.getRecords()) {
            WxCustomerVo vo = new WxCustomerVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            vo.setShopName(shopMapper.selectById(item.getShopId()).getName());
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客服新增
     * @param createValidate
     */
    @Override
    public void add(WxCustomerCreateValidate createValidate) {
        Integer shopId = LikeAdminThreadLocal.getShopId();


        WxCustomer wxCustomer1 = wxCustomerMapper.selectOne(
                new QueryWrapper<WxCustomer>()
                        .eq("shop_id", shopId)
                        .last("limit 1"));

        Assert.isNull(wxCustomer1, "本商铺已存在客服");

        WxCustomer wxCustomer = new WxCustomer();
        wxCustomer.setCustomerUrl(createValidate.getCustomerUrl());
        wxCustomer.setWxId(createValidate.getWxId());
        wxCustomer.setWxName(createValidate.getWxName());
        wxCustomer.setShopId(shopId);
        wxCustomer.setCreateTime(String.valueOf((System.currentTimeMillis() / 1000)));
        wxCustomer.setUpdateTime(String.valueOf((System.currentTimeMillis() / 1000)));
        wxCustomerMapper.insert(wxCustomer);
    }

    /**
     * 客服修改
     * @param updateValidate
     */
    @Override
    public void edit(WxCustomerUpdateValidate updateValidate) {
        WxCustomer wxCustomer = new WxCustomer();
        wxCustomer = wxCustomerMapper.selectById(updateValidate.getId());
        wxCustomer.setCustomerUrl(updateValidate.getCustomerUrl());
        wxCustomer.setWxId(updateValidate.getWxId());
        wxCustomer.setWxName(updateValidate.getWxName());
        wxCustomerMapper.updateById(wxCustomer);
    }

    /**
     * 客服修改
     * @param id
     */
    @Override
    public void del(Integer id) {
        wxCustomerMapper.deleteById(id);
    }
}
